package com.origin.baselibrary.fragment

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.origin.utils.log.LogUtils


@Deprecated("容易造成内存泄露")
fun <f : BaseFragment<*>> Class<f>.getInstance(title: String): f {
    val sf = newInstance()
    sf.mFragmentTitle = title
    return sf
}

abstract class BaseFragment<VB : ViewDataBinding>(var layoutId: Int) : Fragment() {

    var mFragmentTitle: String = ""
    val mContext: Context by lazy {
        context as Context
    }
    lateinit var mBinding: VB

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = DataBindingUtil.inflate(inflater, layoutId, container, false)
        return mBinding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        initUI()

        initDataBinding()


        pageLogical()

        LogUtils._e("显示标题  onViewCreated  $mFragmentTitle", isShowStackTrace = false)

        if (isUserVisible) {
            showHintFragment(isUserVisible)
        }


    }

    var isUserVisible = false


    /**
     * 使用viewpager的时候 调用
     *
     * @param isVisibleToUser
     */
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
//        super.setUserVisibleHint(isVisibleToUser)
        LogUtils.__e("显示标题  xx setUserVisibleHint $isVisibleToUser     ")
        isUserVisible = isVisibleToUser
        if (context == null) {
            return
        }
        showHintFragment(isVisibleToUser)
    }

    /**
     * 不用viewpager 进行管理的时候 调用
     *
     * @param hidden
     */
    override fun onHiddenChanged(hidden: Boolean) {
        LogUtils.__e("显示标题  xx onHiddenChanged $hidden     ")
        showHintFragment(!hidden)
    }


    protected abstract fun initUI()

    open fun initDataBinding() {
    }

    protected abstract fun pageLogical()

    // ture 是显示
    protected abstract fun showHintFragment(isVisibleToUser: Boolean)


}